import React, { Component } from 'react';
import './index.less';
import { base, promptBox, toast, cacheTools } from 'nc-lightapp-front';
import ReferSearch from 'src/hrpub/common/components/referSearch/org';
import NCBackBtn from 'src/hrpub/common/components/hr-back/index';

const { NCSelect } = base;
const NCOption = NCSelect.NCOption;

class Header extends Component {
	constructor(props) {
		super(props);
		this.state = {
			printData: {},
			backBtnVisible: false
		};
		this.updateOrgValue = this.updateOrgValue.bind(this);
		this.updateAgreeType = this.updateAgreeType.bind(this);
		this.onBtnOperation = this.onBtnOperation.bind(this);
		this.goBack = this.goBack.bind(this);
	}

	//更新组织信息
	updateOrgValue(value) {
		this.props.updateOrgValue(value);
	}

	//更新协议类型信息
	updateAgreeType = (value) => {
		this.props.updateAgreeType(value);
	};

	//按钮点击事件
	onBtnOperation(props, btncode, areacode, opt) {
		this.props.onBtnClickOperation(btncode);
	}

	//卡片翻页功能
	handlePageChange(props, pks) {
		this.props && this.props.handleCardPagination(pks);
	}

	//返回按钮执行
	goBack() {
		let { form } = this.props;
		// form.EmptyAllFormValue('agreementCard');
		// this.props.updateState({
		//     btncode: 'back',
		//     pageFlag: 'list',
		//     isEdit: false
		// });

		if (this.props.isEdit) {
			this.props.cancelButtonClick && this.props.cancelButtonClick('cancel');
		} else {
			this.props.updateState(
				{
					btncode: 'back',
					pageFlag: 'list',
					isEdit: false
				},
				() => {
					this.props.setMainFormId();
				}
			);
		}
	}

	// 设置卡片分页显示隐藏
	setCardPaginationVisible(paginationShow, pk_agreement) {
		this.setState(
			{
				paginationShow
			},
			function() {
				if (pk_agreement) {
					let { cardPagination } = this.props;
					cardPagination.setCardPaginationId({ id: pk_agreement, status: 1 });
				}
			}
		);
	}

	render() {
		const { button, orgVal, dataList, pageFlag, isEdit, cardPagination, json, agreementType } = this.props;
		const { createCardPagination } = cardPagination;
		return (
			<div>
				<div className="header nc-bill-header-area">
					<div style={{ display: pageFlag !== 'list' ? true : 'none' }}>
						<NCBackBtn onClick={this.goBack} />
					</div>
					<div className="header-refer" style={{ display: pageFlag === 'list' ? true : 'none' }}>
						{agreementType&&<ReferSearch orgVal={orgVal} getOrgData={this.updateOrgValue} />}
					</div>
					<div>
						{json['cm6011-000115'] && dataList.length > 0 ? (
							<NCSelect
								showSearch
								style={{ width: 150, marginLeft: 10, display: pageFlag === 'list' ? true : 'none' }}
								optionFilterProp="children"
								defaultValue={dataList[0].name} /*岗位协议*/
								placeholder={json['cm6011-000114']} /*协议类型*/
								onSelect={this.onSelect}
								onChange={this.updateAgreeType}
							>
								{dataList.map((da) => (
									<NCOption key={da.code} value={da.code} item={da}>
										{da.name}
									</NCOption>
								))}
							</NCSelect>
						) : (
							''
						)}
					</div>
					<div className="btn-group">
						<div className="definition-icons">
							{button.createButtonApp({
								area: 'head',
								onButtonClick: (props, btncode) => {
									this.onBtnOperation(props, btncode, 'head', this);
								}
							})}
						</div>
					</div>
					{pageFlag === 'card' && !isEdit ? (
						<div className="pagination-card">
							{createCardPagination({
								handlePageInfoChange: this.handlePageChange.bind(this),
								urlPkname: 'id'
							})}
						</div>
					) : (
						null
					)}
				</div>
			</div>
		);
	}
}

export default Header;
